package com.example.form.controller;

import com.example.form.common.AppResult;
import com.example.form.common.ResultCode;
import com.example.form.config.AppConfig;
import com.example.form.exception.ApplicationException;
import com.example.form.model.Article;
import com.example.form.model.ArticleReply;
import com.example.form.model.User;
import com.example.form.service.IArticleReplyService;
import com.example.form.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "回复接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {
    @Resource
    private IArticleService articleService;//属性注入得到操作帖子表的对象
    @Resource
    private IArticleReplyService articleReplyService;//..........
    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("帖子id") @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam("回复的内容") @RequestParam("content") @NonNull String content){//校验参数了
        //获取用户
        HttpSession session=request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        //判断用户是否已经禁言
        if (user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);//返回已经被禁言异常
        }
        //获取要回复的帖子的对象(为了等下设置回复数量加一)
        Article article=articleService.selectById(articleId);
        //检验帖子状态.是否存在或者被删除
        if (article.getDeleteState()==1||article==null){
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);//返回资源不存在异常
        }
        //当前帖子是否处于封贴状态,处于则不能回复了
        if(article.getState()==1){
            return AppResult.failed(ResultCode.FAILED);//返回操作失败异常
        }
        //构造回复对象
        //构造回复的对象
        ArticleReply articleReply=new ArticleReply();
        articleReply.setArticleId(articleId);//关联的帖子id
        articleReply.setPostUserId(user.getId());//回复的用户id
        articleReply.setContent(content);//回复的内容
        //执行回复方法
        articleReplyService.creat(articleReply);
        //返回结果
        return AppResult.success();
    }
    @ApiOperation("获取帖子回复列表")
    @GetMapping("/getRepLies")
    public AppResult<List<ArticleReply>> getRepLiesByArticleld(@ApiParam("帖子id") @RequestParam("articleId")@NonNull Long articleId){//参数校验了
        //校验帖子是否存在
        Article article=articleService.selectById(articleId);//根据帖子id获取对应的帖子的信息
        if(article==null||article.getDeleteState()==1){
            //不存在或者被删除
            //打印日志
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));//抛出资源不存在异常
        }
        //调用service
        List<ArticleReply> articleReplies=articleReplyService.selectByArticleId(articleId);
        //返回结果
        return AppResult.success(articleReplies);
    }
}
